﻿using System;
using System.Collections.Generic;
using Moq;
using Prompts.PromptServiceProxy;
using Prompts.Service.PromptService;

namespace Test.Prompts.Infrastructure.Fakes
{
    internal class FakePromptServiceClient
    {
        private readonly Mock<IPromptServiceClient> _promptServiceClient;
        private readonly Dictionary<string, Action<GetPromptsResponse>> _callbackDictionary;

        public FakePromptServiceClient(Mock<IPromptServiceClient> promptServiceClient)
        {
            _promptServiceClient = promptServiceClient;
            _callbackDictionary = new Dictionary<string, Action<GetPromptsResponse>>();
        }

        public void SetupGetPromptsAsync(string reportPath)
        {
            var setup = _promptServiceClient.Setup(c => c.GetPromptsForReportAsync(
                reportPath
                , It.IsAny<Action<GetPromptsResponse>>()));

            setup.Callback<string, Action<GetPromptsResponse>>(
                (s, callback) =>
                    {
                        if (!_callbackDictionary.ContainsKey(reportPath))
                        {
                            _callbackDictionary.Add(reportPath, callback);
                        }

                    });
        }

        public void RaiseGetPromptsCompleted(IEnumerable<PromptInfo> promptInfos, string reportPath)
        {
            Action<GetPromptsResponse> callback;

            _callbackDictionary.TryGetValue(reportPath, out callback);

            callback(
                new GetPromptsResponse
                {
                    ErrorMessage = string.Empty
                    ,
                    ErrorOccured = false
                    ,
                    Prompts = promptInfos
                });
        }

        public void RaiseGetPromptsCompletedWithError(string errorMessage, string reportPath)
        {
            Action<GetPromptsResponse> callback;

            _callbackDictionary.TryGetValue(reportPath, out callback);

            callback(
                new GetPromptsResponse
                {
                    ErrorMessage = errorMessage
                    ,
                    ErrorOccured = true
                    ,
                    Prompts = new PromptInfo[] { }
                });
        }
    }
}
